package com.atguigu.spring.aop.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author yanglin
 * @create 2023-12-12 16:27 星期二
 * description:
 */
@Component
@Aspect
@Order(1)   //设置增强方法执行的优先级  数字越小，优先级越高
public class PersonProxy {

    //相同切入点抽取
    @Pointcut(value = "execution(* com.atguigu.spring.aop.anno.AopUser.add(..))")
    public void pointDemo(){

    }


    /**
     * 前置通知  @Before()
     */
    //@Before(value = "execution(* com.atguigu.spring.aop.anno.AopUser.add(..))")    //切入点表达式
    @Before(value = "pointDemo()")
    public void before(){
        System.out.println("person before....");
    }

    //最终通知  （有异常也执行）
    //@After(value = "execution(* com.atguigu.spring.aop.anno.AopUser.add(..))")
    @After(value = "pointDemo()")
    public void after(){
        System.out.println("person after.....");
    }

    //后置通知  （返回通知）  （有异常就不执行）
    //@AfterReturning(value = "execution(* com.atguigu.spring.aop.anno.AopUser.add(..))")
    @AfterReturning(value = "pointDemo()")
    public void afterReturning(){
        System.out.println("person afterReturning......");
    }

    //异常通知   （只有发生异常才会执行）
    //@AfterThrowing(value = "execution(* com.atguigu.spring.aop.anno.AopUser.add(..))")
    @AfterThrowing(value = "pointDemo()")
    public void afterThrowing() {
        System.out.println("person afterThrowing....");
    }

    //环绕通知
    //@Around(value = "execution(* com.atguigu.spring.aop.anno.AopUser.add(..))")
    @Around(value = "pointDemo()")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("person 环绕之前......");
        //被增强的方法执行
        proceedingJoinPoint.proceed();
        System.out.println("person 环绕之后......");
    }
}
